package com.newly.center.business.service.impl;

import cn.hutool.core.date.DatePattern;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.NumberUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.newly.center.business.service.StatisticsDayService;
import com.newly.common.base.entity.business.Po.Site;
import com.newly.common.base.entity.business.Po.StatisticsDay;
import com.newly.common.base.entity.business.Vo.*;
import com.newly.common.mapper.business.SiteMapper;
import com.newly.common.mapper.business.StatisticsDayMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @anthor sbq
 * @date 2021/11/4 9:46
 */
@Service("statisticsDayService")
public class StatisticsDayServiceImpl extends ServiceImpl<StatisticsDayMapper, StatisticsDay> implements StatisticsDayService {

    @Autowired
    private SiteMapper siteMapper;

    @Override
    public List<LastXDayTrendVo> lastXDayTrend(Integer offset) {
        List<LastXDayTrendVo> list = new ArrayList<>();
        Integer num;
        String date = DateUtil.offsetDay(DateUtil.date(),-offset).toString(DatePattern.NORM_DATE_FORMAT);
        List<StatisticsDay> statisticsDayList = this.list(new QueryWrapper<StatisticsDay>().lambda().ge(StatisticsDay::getCreateTime,date));
        //按日期分组
        Map<String,List<StatisticsDay>> map = statisticsDayList.stream().collect(Collectors.groupingBy(StatisticsDay::getCreateTime));
        for (Map.Entry<String, List<StatisticsDay>> entry : map.entrySet()) {
            LastXDayTrendVo lastXDayTrendVo = new LastXDayTrendVo();
            statisticsDayList = entry.getValue();
            lastXDayTrendVo.setDate(entry.getKey());
            num = statisticsDayList.stream().mapToInt(StatisticsDay::getCheckNum).sum();
            lastXDayTrendVo.setTotalNum(num);
            num = statisticsDayList.stream().mapToInt(StatisticsDay::getNormalNum).sum();
            lastXDayTrendVo.setNormalNum(num);
            num = statisticsDayList.stream().mapToInt(StatisticsDay::getOverweightNum).sum();
            lastXDayTrendVo.setOverweightNum(num);
            list.add(lastXDayTrendVo);
        }
        return list;
    }

    @Override
    public List<LastXDayOverweightVo> lastXDayOverweight(Integer offset) {
        List<LastXDayOverweightVo> list = new ArrayList<>();
        Integer num;
        String date = DateUtil.offsetDay(DateUtil.date(),-offset).toString(DatePattern.NORM_DATE_FORMAT);
        List<StatisticsDay> statisticsDayList = this.list(new QueryWrapper<StatisticsDay>().lambda().ge(StatisticsDay::getCreateTime,date));
        //按日期分组
        Map<String,List<StatisticsDay>> map = statisticsDayList.stream().collect(Collectors.groupingBy(StatisticsDay::getCreateTime));
        for (Map.Entry<String, List<StatisticsDay>> entry : map.entrySet()) {
            LastXDayOverweightVo lastXDayOverweightVo = new LastXDayOverweightVo();
            statisticsDayList = entry.getValue();
            lastXDayOverweightVo.setDate(entry.getKey());
            num = statisticsDayList.stream().mapToInt(StatisticsDay::getOverweightNum).sum();
            lastXDayOverweightVo.setOverweightNum(num);
            list.add(lastXDayOverweightVo);
        }
        return list;
    }

    @Override
    public List<LastXDayRankingVo> lastXDayRanking(Integer offset) {
        List<LastXDayRankingVo> list = new ArrayList<>();
        String date = DateUtil.offsetDay(DateUtil.date(),-offset).toString(DatePattern.NORM_DATE_FORMAT);
        List<StatisticsDay> statisticsDays = this.list(new QueryWrapper<StatisticsDay>().lambda().ge(StatisticsDay::getCreateTime,date));
        //按站点名称分组
        Map<String,List<StatisticsDay>> map = statisticsDays.stream().collect(Collectors.groupingBy(StatisticsDay::getSiteName));
        for (Map.Entry<String,List<StatisticsDay>> entry : map.entrySet()){
            LastXDayRankingVo lastXDayRankingVo = new LastXDayRankingVo();
            statisticsDays = entry.getValue();
            int totalNum = statisticsDays.stream().mapToInt(StatisticsDay::getCheckNum).sum();
            int overweightNum = statisticsDays.stream().mapToInt(StatisticsDay::getOverweightNum).sum();
            Double ratio;
            if (totalNum == 0)
                ratio = 0.00;
            else
                ratio = NumberUtil.div((double) overweightNum,(double)totalNum,4);
            lastXDayRankingVo.setSiteName(entry.getKey());
            lastXDayRankingVo.setTotalNum(totalNum);
            lastXDayRankingVo.setOverweightNum(overweightNum);
            lastXDayRankingVo.setOverweightPercent(ratio);
            list.add(lastXDayRankingVo);
        }
        return list;
    }

    @Override
    public List<AxleCountVo> getAxleRanking(List<StatisticsDay> statisticsDayList) {
        List<AxleCountVo> axleCountVos = new ArrayList<>();
        //按轴数分组
        Map<Integer,List<StatisticsDay>> map = statisticsDayList.stream().collect(Collectors.groupingBy(StatisticsDay::getAxleNum));
        for (Map.Entry<Integer,List<StatisticsDay>> entry : map.entrySet()){
            AxleCountVo axleCountVo = new AxleCountVo();
            statisticsDayList = entry.getValue();
            axleCountVo.setAxleNum(entry.getKey());
            Double rate = statisticsDayList.stream().mapToDouble(StatisticsDay::getRate).average().orElse(0.00);
            axleCountVo.setAvgOverweightRate(rate);
            axleCountVo.setAxleNum(statisticsDayList.size());
            axleCountVos.add(axleCountVo);
        }
        return axleCountVos;
    }

    @Override
    public MapVo map() {
        MapVo mapVo = new MapVo();
        List<MapSiteVo> mapSiteVos = new ArrayList<>();
        //今天
        String today = DateUtil.today();
        Integer siteNum = this.siteMapper.selectList(new QueryWrapper<Site>().lambda().eq(Site::getIsDeleted,0).orderByAsc(Site::getId)).size();
        //站点总数
        mapVo.setSiteNum(siteNum);
        List<StatisticsDay> todayList = this.list(new QueryWrapper<StatisticsDay>().lambda().eq(StatisticsDay::getCreateTime,today));
        //今日超重数
        int todayOverweightNum = todayList.stream().mapToInt(StatisticsDay::getOverweightNum).sum();
        mapVo.setTodayOverweightNum(todayOverweightNum);
        int todayCheckNum = todayList.stream().mapToInt(StatisticsDay::getCheckNum).sum();
        //今日检测总数
        mapVo.setTodayTotalNum(todayCheckNum);
        double ratio;
        if (todayCheckNum == 0)
            ratio = 0.00;
        else
            ratio = NumberUtil.div((double)todayOverweightNum,(double) todayCheckNum,4);
        //今日超重百分比
        mapVo.setTodayOverweightPercent(ratio);
        //按站点名称分组
        Map<String,List<StatisticsDay>> map = todayList.stream().collect(Collectors.groupingBy(StatisticsDay::getSiteName));
        for (Map.Entry<String,List<StatisticsDay>> entry : map.entrySet()){
            todayList = entry.getValue();
            MapSiteVo mapSiteVo = new MapSiteVo();
            mapSiteVo.setSiteName(entry.getKey());
            //一个站点每天只有一条数据
            mapSiteVo.setTodayTotalNum(todayList.get(0).getCheckNum());
            mapSiteVo.setTodayOverweightNum(todayList.get(0).getOverweightNum());
            mapSiteVos.add(mapSiteVo);
        }
        mapVo.setMapSiteVoList(mapSiteVos);
        return mapVo;
    }

}
